﻿using Face8280WebSocketServer.Interface;
using Face8280WebSocketServer.Model;
using Face8280WebSocketServer.Utility;
using Face8280WebSocketServer.Utility.FilterAttribute;
using Face8280WebSocketServer.Utility.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Face8280WebSocketServer.Controllers.FaceDevice
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeviceReportController : ControllerBase
    {

        private readonly ILogger<DeviceReportController> _logger;
        private IMyMemoryCache _Cache;
        private IRecordService _RecordDB;

        public DeviceReportController(ILogger<DeviceReportController> logger,
            IRecordService door, IMyMemoryCache mycache)
        {
            _logger = logger;
            _RecordDB = door;
            _Cache = mycache;
        }


        /// <summary>
        /// 获取设备出入记录
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetReport")]
        [Authorize]
        [TypeFilter(typeof(VerifyActionFilterAttribute))]
        public IActionResult GetReport([FromForm] DeviceReportQueryParameter par)
        {

            List<Expression<Func<DriveRecord, bool>>> oWheres = new List<Expression<Func<DriveRecord, bool>>>();
            oWheres.Add(x => x.NoteTime >= par.ReportBegin && x.NoteTime <= par.ReportEnd);

            if (!string.IsNullOrWhiteSpace(par.DeviceID)) oWheres.Add(x => x.DeviceID.Contains(par.DeviceID));
            if (par.EmployeeID > 0) oWheres.Add(x => x.EmployeeID == par.EmployeeID);
            if (par.eventType > 0) oWheres.Add(x => x.EventType == par.eventType);

            _logger.LogInformation("获取出入记录列表");
            var reports = _RecordDB.QueryPage(
                oWheres, par.pageSize, par.pageIndex,
                x => x.NoteTime,
                par.isAsc);

            return new JsonResult(new JsonResultModel(reports));
        }

        /// <summary>
        /// 清空出入记录
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("ClearReport")]
        [Authorize]
        public IActionResult ClearReport()
        {


            _logger.LogInformation("清空出入记录");
            _RecordDB.ClearRecord();

            Task.Run(DeleteAllRecordImage);

            return new JsonResult(new JsonResultModel());
        }

        /// <summary>
        /// 删除所有记录图片
        /// </summary>
        private void DeleteAllRecordImage()
        {
            try
            {
                string sPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "RecordImage");
                if (!Directory.Exists(sPath))
                {
                    //Directory.CreateDirectory(sPath);
                    return;
                }
                var sDirs = Directory.GetDirectories(sPath);
                foreach (var sDir in sDirs)
                {
                    var sFiles = Directory.GetFiles(sDir, "*.jpg");
                    foreach (var sfile in sFiles)
                    {
                        try
                        {
                            System.IO.File.Delete(sfile);
                        }
                        catch (Exception)
                        {

                            ;
                        }
                        
                    }

                }
            }
            catch (Exception)
            {

                ;
            }
        }

    }
}
